<?php
//状态模式

abstract class State{
    private $context;

    public function setcontext(Context $context){
        $this->context=$context;
    }

    public function getcontext(){
        return $this->context;
    }

    public abstract function open();
    public abstract function close();
    public abstract function run();
    public abstract function stop();
}

class OpenState extends State{
    public function open()
    {
       echo "电梯门开启中。。。。</br>";
    }

    public function close()
    {
        $this->getcontext()->setLiftState($this->getcontext()->closeState);
        $this->getcontext()->getLiftState()->close();
    }

    public function run()
    {
        // TODO: Implement run() method.
    }

    public function stop()
    {
        // TODO: Implement stop() method.
    }
}

class CloseState extends State{
    public function open()
    {
        $this->getcontext()->setLiftState($this->getcontext()->openState);
        $this->getcontext()->getLiftState()->open();
    }

    public function close()
    {
        echo "电梯正在关门。。。</br>";
    }

    public function run()
    {
        $this->getcontext()->setLiftState($this->getcontext()->runState);
        $this->getcontext()->getLiftState()->run();
    }

    public function stop()
    {
        $this->getcontext()->setLiftState($this->getcontext()->stopState);
        $this->getcontext()->getLiftState()->stop();
    }
}


class RunState extends State{
    public function open()
    {

    }

    public function close()
    {
        // TODO: Implement close() method.
    }

    public function run()
    {
        echo "电梯正在上下跑。。。</br>";
    }

    public function stop()
    {
        $this->getcontext()->setLiftState($this->getcontext()->stopState);
        $this->getcontext()->getLiftState()->stop();
    }
}


class StopState extends State{
    public function open()
    {
        $this->getcontext()->setLiftState($this->getcontext()->openState);
        $this->getcontext()->getLiftState()->open();
    }

    public function close()
    {
        // TODO: Implement close() method.
    }

    public function run()
    {
        $this->getcontext()->setLiftState($this->getcontext()->runState);
        $this->getcontext()->getLiftState()->run();
    }

    public function stop()
    {
        echo "电梯正在停止。。。</br>";
    }
}



class Context{
    public $openState;
    public  $closeState;
    public  $runState;
    public  $stopState;
    
    private $liftState;


    public function __construct(){
        $this->openState = new OpenState();
        $this->closeState = new CloseState();
        $this->runState = new RunState();
        $this->stopState = new StopState();
    }
    
    public function setLiftState($liftState){
        $this->liftState=$liftState;
        $this->liftState->setContext($this);
    }
    
    public function getLiftState(){
        return $this->liftState;
    }

    public function open(){
        $this->liftState->open();
    }

    public function close(){
        $this->liftState->close();
    }

    public function run(){
        $this->liftState->run();
    }

    public function stop(){
        $this->liftState->stop();
    }
}


echo "<meta charset=UTF-8>";
$context = new Context();
$context->setLiftState(new CloseState());
$context->open();
$context->close();
$context->run();
$context->stop();
?>